﻿/*************************************************************************
 *  Copyright © 2023-2030 FXB CO.,LTD. All rights reserved.
 *------------------------------------------------------------------------
 *  公司：DefaultCompany
 *  项目：CustomBuildProcess
 *  文件：LogQuickRecord.cs
 *  作者：Administrator
 *  日期：2025/6/20 10:47:30
 *  功能：Nothing
*************************************************************************/

using UnityEditor;
using UnityEngine;

namespace CustomBuildProcess.Editor
{
    public class LogQuickRecordWindow : EditorWindow
    {
        public static LogQuickRecordWindow instance;
        public BuildInfoConfig configAsset;
        public BuildConfigItem logConfigItem;

        private Vector2 scrollView;
        public string log;
        private int selectedIndex;

        [MenuItem("Window/Log Quick Record &_L")]
        public static void ShowWindow()
        {
            instance = GetWindow<LogQuickRecordWindow>("Log Quick Record");
            instance.minSize = new Vector2(500, 300);
            instance.Show();
        }

        private void OnEnable()
        {
            configAsset = AssetDatabase.LoadAssetAtPath<BuildInfoConfig>(PathManager.BuildConfigPath);
            logConfigItem = Resources.Load<BuildConfigItem>(PathManager.BuildConfigItemPath);

            ImportLogFromCuttingBoardLog();
        }

        private void OnGUI()
        {
            scrollView = EditorGUILayout.BeginScrollView(scrollView);

            log = EditorGUILayout.TextArea(log, GUILayout.ExpandHeight(true));
            Undo.RecordObject(this, "Log Changed");

            EditorGUILayout.EndScrollView();

            EditorGUILayout.BeginHorizontal();
            if (DropdownButton("Import", new GUIStyle("button"), out bool isArrow))
            {
                if (isArrow)
                {
                    ShowMenu();
                }
                else
                {
                    ImportLogFromCuttingBoardLog();
                    if (string.IsNullOrEmpty(logConfigItem.cuttingBoardLog))
                        ShowNotification(new GUIContent("Cutting board has no information"));
                    else
                        ShowNotification(new GUIContent("Import successful"));
                }
            }

            if (GUILayout.Button("Export"))
            {
                logConfigItem.cuttingBoardLog = log;
                EditorUtility.SetDirty(logConfigItem);
                AssetDatabase.SaveAssets();
                ShowNotification(new GUIContent("Export successful"));
            }

            if (GUILayout.Button("Clear"))
            {
                log = "";
                logConfigItem.cuttingBoardLog = "";
                EditorUtility.SetDirty(logConfigItem);
                AssetDatabase.SaveAssets();
                ShowNotification(new GUIContent("Clear successful"));
            }
            EditorGUILayout.EndHorizontal();
        }

        public static bool DropdownButton(string label, GUIStyle style, out bool isArrowClicked, params GUILayoutOption[] options)
        {
            GUIContent content = new GUIContent(label);

            style.alignment = TextAnchor.MiddleCenter;
            style.padding.left = 10;

            Rect rect = GUILayoutUtility.GetRect(content, style, options);
            float arrowWidth = 20f;
            Rect arrowRect = new Rect(rect.xMax - arrowWidth, rect.y, arrowWidth, rect.height);

            Event current = Event.current;

            GUI.Box(rect, content, style);

            Handles.color = new Color(0.137f, 0.137f, 0.137f, 1F);
            Handles.DrawLine(new Vector2(arrowRect.x, rect.y), new Vector2(arrowRect.x, rect.y + rect.height));

            GUI.Label(arrowRect, EditorGUIUtility.IconContent("Icon Dropdown"), new GUIStyle("Label") { alignment = TextAnchor.MiddleCenter });

            isArrowClicked = false;

            if (current.type == EventType.MouseDown && rect.Contains(current.mousePosition))
            {
                if (arrowRect.Contains(current.mousePosition))
                {
                    isArrowClicked = true;
                }

                current.Use();
                return true;
            }

            return false;
        }

        private void ShowMenu()
        {
            GenericMenu menu = new GenericMenu();

            string[] versionsDisplayName = configAsset.GetAllVersionsDisplayName();
            for (int i = 0; i < versionsDisplayName.Length; i++)
            {
                int index = i;
                menu.AddItem(new GUIContent(versionsDisplayName[i]), selectedIndex == index, () => OnItemSelected(index));
            }

            menu.ShowAsContext();
        }

        private void OnItemSelected(int index)
        {
            selectedIndex = index;
            ImportLogFromBuildInfo(index);
        }

        private void ImportLogFromBuildInfo(int index)
        {
            log = configAsset.GetBuildInfo(index).log;
            SceneView.lastActiveSceneView?.Focus();
        }

        private void ImportLogFromCuttingBoardLog()
        {
            if (!string.IsNullOrEmpty(logConfigItem.cuttingBoardLog))
                log = logConfigItem.cuttingBoardLog;
            SceneView.lastActiveSceneView?.Focus();
        }
    }
}